package com.yitiao.data_algorithms.leetcode.editor.cn;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class KthSmallestElementInABst{
    // 2022-08-10 17:26:59
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int ans = 0;
    int index = 1;
    public int kthSmallest(TreeNode root, int k) {
        dfs(root,k);
        return ans;
    }
    void dfs(TreeNode root,int k){
        if (root == null)return;
        dfs(root.left,k);
        if (index == k){
            ans = root.val;
        }
        index++;
        dfs(root.right,k);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}